<?php

namespace app\admin\controller\annalgame;

use app\common\controller\Backend;

use think\Controller;
use think\Request;
use think\Db;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Annalroom extends Backend
{   
      /**
     * 跨库关联查询关联键
    */
    protected $kuaid = 'uid';

    /**
     * 跨库关联读取关建键表
     */
    protected $kuatablea = 'fa_v_annal_room';

    /**
     * 跨库关联提取字段表
     */
    protected $kuatableb = 'user';

    /**
     * 跨库关联提取字段字符串
     */
    protected $fieldstr= 'uid,nickname';

    
    /**
     * VAnnalRoom模型对象
     */
    protected $model = null;

    public function _initialize()
    { 
        parent::_initialize();
        $this->model = model('VAnnalRoom');
        $this->view->assign("statusList", $this->model->getStatuslist());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
     * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);

        //进行获取信息
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {   
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            
            //搜索条件
            $filter = $this->request->get("filter", '');
            $filter_arr = json_decode($filter,true);
            $map = [];
            foreach ($filter_arr as $key=>$val){
                if($key == 'createtime'){
                    $time_arr = explode(',', $filter_arr['createtime']);
                    $star = $this->getMilliDate($time_arr[0]);
                    $end = $this->getMilliDate($time_arr[1]);
                    if(empty($time_arr[0])){
                        $map['createtime'] = array('<=', $end);
                    }elseif(empty($time_arr[1])){
                        $map['createtime'] = array('>=', $star);
                    }else{
                        $map['createtime'] = array('between', [$star,$end]);
                    }
                }else{
                    $map[$key] = $val;
                }
            }
            $map['clubId'] = 0;

            /*
             * 统计数据
             */
            $statistics = [];
            $map_roomcode['roomcode'] = array('>',0);
            $map_roomcode['clubId'] = 0;
            $map_roomcode = array_merge($map_roomcode,$map);
            //房卡消耗数量
            $roomcodenum = $this->model->field('sum(roomcode) as roomcodenum')->where($map)->find();
            if(empty($roomcodenum->roomcodenum)){
                $statistics['roomcodenum'] = 0;
            }else{
                $statistics['roomcodenum'] = $roomcodenum->roomcodenum;
            }
            //总房间数
            $room_num = $this->model->field('roomid')->where($map_roomcode)->group('roomid,createtime')->count();
            $statistics['room_num'] = $room_num;
            //总游戏时长
            $subQuery = Db::table('fa_v_annal_room')->field('(endtime-createtime) as time_diff')
            ->where($map_roomcode)->group('roomid,createtime')->buildSql();
            $room_time_count = Db::table($subQuery.' a')->field('sum(a.time_diff) as time_count')->find();
            if(empty($room_time_count['time_count'])){
                $statistics['time_count'] = 0;
            }else{
                $statistics['time_count'] = $this->Sec2Time($room_time_count['time_count']);
            }
            //平均游戏时长
            $room_time_avg = Db::table($subQuery.' a')->field('avg(a.time_diff) as time_avg')->find();
            if(empty($room_time_avg['time_avg'])){
                $statistics['time_avg'] = 0;
            }else{
                $statistics['time_avg'] = $this->Sec2Time($room_time_avg['time_avg']);
            }
            
            //总数
            $total = $this->model
                    ->where($map)
                    ->order($sort, $order)
                    ->count();
           
            $list = $this->model
                    ->where($map)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();    
                    
              //加入游戏时长      
              foreach($list as $value){ 
                if (!empty($value['endtime']))
                {   
                   $gametime_new = $this->timeDiff($value['createtime'],$value['endtime']);
                   $value['gametime'] = $this->Sec2Time($gametime_new);
                }
            }

            //关联查询数据       
            $result = array("total" => $total, "rows" => $this->SetNamepost($this->kuakfunc(),$list),"statistics"=>$statistics,"map"=>empty($map)?0:1);

            return json($result);
        }

        return $this->view->fetch();
    }    
    
}
